package com.mender.trade.service.impl;

import com.mender.api.client.ProductClient;
import com.mender.common.context.BaseContext;
import com.mender.common.result.Result;
import com.mender.domain.po.Setmeal;
import com.mender.domain.vo.DishVO;
import com.mender.trade.domain.dto.ShoppingCartDTO;
import com.mender.trade.domain.po.ShoppingCart;
import com.mender.trade.mapper.ShoppingCartMapper;
import com.mender.trade.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private ProductClient productClient;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
//        判断当前商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //        如果存在，则修改数量
        if (list.size() > 0 && list != null) {
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart1);
        } else {
            //        如果不存在，则新增
//            判断本次添加是菜品还是套餐
            if (shoppingCartDTO.getSetmealId() != null) {
//                套餐
                Result<List<Setmeal>> resultSetmeals =
                        productClient.selectByIds(Collections.singletonList(shoppingCartDTO.getSetmealId()));
                List<Setmeal> setmeals = resultSetmeals.getData();
                Setmeal setmeal = setmeals.get(0);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }else {
                //                菜品
                Result<DishVO> resultDish = productClient.getDishById(shoppingCartDTO.getDishId());
                DishVO dish = resultDish.getData();
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                        .build();
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }
}
